'use strict';
const { Op } = require('sequelize');

function toInt(str) {
    if (typeof str === 'number') return str;
    if (!str) return str;
    return parseInt(str, 10) || 0;
}
const { Service } = require('egg');
class MenuService extends Service {
    async index() {
        const {ctx} = this;
        const Menu=ctx.model.Menu
        const {size,current}=ctx.query
        const limit=ctx.helper.getCurrent({size,current})
        const query = {
            ...limit
        };
        const res=await Menu.findAll(query)
        let result=[];
        let map = {};
        res.forEach(item=>{
            map[item.dataValues.id]= item.dataValues
        })
        res.forEach(item => {
            // item.pid 为0时 返回underfined
            let parent = map[item.dataValues.pid];
            if (parent) {
                item.dataValues.parentName=parent.name;
                (parent.children || (parent.children = [])).push(item.dataValues);
            } else {
                // 这里push的item是pid为0的数据
                result.push(item.dataValues);
            }
        });
        let count = await ctx.model.Menu.count();
        return {count,data:result}
    }
    async add(){
        const ctx = this.ctx;
        const { name, path,pid,status } = ctx.request.body;
        const Menu=ctx.model.Menu
        const menuInfo = await Menu.findOne({
            where:{
                name
            }
        });
        let sign=""
        let data={name,path,status}
        if (!menuInfo){
            if (pid) {
                data.pid=pid
                data.sign='layout'
            }
            const user = await Menu.create(data);
            if (user){
                return {code:0,data:null,message:'新建成功'}
            }else{
                return {code:1,data:null,message:'服务器错误请稍后再试'}
            }
        }else{
            return {code:1,data:null,message:'菜单重复'}
        }

    }
    async show() {
        const ctx = this.ctx;
        const query = {
            where:{
                id:ctx.query.id
            }
        };
        let {dataValues}=await ctx.model.Menu.findOne(query)
        return {data:dataValues}
    }
    async edit() {
        const ctx = this.ctx;
        const data=ctx.request.body
        const id = toInt(data.id);
        const menu = await ctx.model.Menu.findByPk(id);
        if (!menu) {
            ctx.status = 404;
            return;
        }
        if (!data.pid){
            data.pid=null
        }
        await menu.update(data);
        return {data:null,message:'修改成功'}
    }
    async destroy() {
        const ctx = this.ctx;
        const Menu=ctx.model.Menu
        const {id}=ctx.request.body
        const res = await Menu.findByPk(id);
        if (!res) {
            return {
                data:null,
                message:'无该数据'
            };
        }
        const result=await res.destroy();
        return { data:result,message:'删除成功'}
    }
    async getParentMenu() {
        const ctx = this.ctx;
        const Menu=ctx.model.Menu

        const res = await Menu.findAll({
            attributes: [['id','value'], ['name','label']],
            where:{
                [Op.and]: [
                    { sign: null },
                    { id:{
                            [Op.ne]: ctx.query.id,
                        }
                    }
                ]
            }
        });
        console.log(ctx.request.res)
        return { data:res}
    }
}

module.exports = MenuService;
